using System;
using System.Data;
using System.Text;
using System.Windows.Forms;
using ctsu.Data;


namespace ctsu.Admin.Forms
{
	public class VisitChangePromptDlg : System.Windows.Forms.Form
	{
		private string _table;
		private int _protocolID;
		private int _newVal;
		private string _field;

		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.TextBox VisitType;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Button btnChange;
		private System.Windows.Forms.Button btnNoChange;
		private System.Windows.Forms.Button btnExit;
		private System.Windows.Forms.DataGrid dg;

		private System.ComponentModel.Container components = null;

		public VisitChangePromptDlg(string table, int protocolID, int newVal, string field)
		{
			this._table = table;
			this._protocolID = protocolID;
			this._newVal = newVal;
			this._field = field;
			InitializeComponent();
		}

		protected override void Dispose(bool disposing)
		{
			if (disposing)
			{
				if (components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose(disposing);
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.label1 = new System.Windows.Forms.Label();
			this.VisitType = new System.Windows.Forms.TextBox();
			this.label2 = new System.Windows.Forms.Label();
			this.dg = new System.Windows.Forms.DataGrid();
			this.btnChange = new System.Windows.Forms.Button();
			this.btnNoChange = new System.Windows.Forms.Button();
			this.btnExit = new System.Windows.Forms.Button();
			((System.ComponentModel.ISupportInitialize)(this.dg)).BeginInit();
			this.SuspendLayout();
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(8, 8);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(64, 16);
			this.label1.TabIndex = 0;
			this.label1.Text = "Visit Type:";
			// 
			// VisitType
			// 
			this.VisitType.Location = new System.Drawing.Point(72, 8);
			this.VisitType.Name = "VisitType";
			this.VisitType.ReadOnly = true;
			this.VisitType.TabIndex = 1;
			this.VisitType.TabStop = false;
			this.VisitType.Text = "";
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(8, 40);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(544, 16);
			this.label2.TabIndex = 2;
			this.label2.Text = "Press \'Update\' or \'No Change\' for the selected row.   The next row will be select" +
				"ed after pressing a button.";
			// 
			// dg
			// 
			this.dg.DataMember = "";
			this.dg.HeaderForeColor = System.Drawing.SystemColors.ControlText;
			this.dg.Location = new System.Drawing.Point(8, 56);
			this.dg.Name = "dg";
			this.dg.ReadOnly = true;
			this.dg.Size = new System.Drawing.Size(576, 120);
			this.dg.TabIndex = 3;
			// 
			// btnChange
			// 
			this.btnChange.Location = new System.Drawing.Point(171, 184);
			this.btnChange.Name = "btnChange";
			this.btnChange.TabIndex = 4;
			this.btnChange.Text = "Update";
			this.btnChange.Click += new System.EventHandler(this.btnChange_Click);
			// 
			// btnNoChange
			// 
			this.btnNoChange.Location = new System.Drawing.Point(259, 184);
			this.btnNoChange.Name = "btnNoChange";
			this.btnNoChange.TabIndex = 5;
			this.btnNoChange.Text = "No Change";
			this.btnNoChange.Click += new System.EventHandler(this.btnNoChange_Click);
			// 
			// btnExit
			// 
			this.btnExit.Location = new System.Drawing.Point(347, 184);
			this.btnExit.Name = "btnExit";
			this.btnExit.TabIndex = 6;
			this.btnExit.Text = "Exit";
			this.btnExit.Click += new System.EventHandler(this.btnExit_Click);
			// 
			// VisitChangePromptDlg
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(592, 214);
			this.Controls.Add(this.btnExit);
			this.Controls.Add(this.btnNoChange);
			this.Controls.Add(this.btnChange);
			this.Controls.Add(this.dg);
			this.Controls.Add(this.label2);
			this.Controls.Add(this.VisitType);
			this.Controls.Add(this.label1);
			this.Name = "VisitChangePromptDlg";
			this.Text = "Visit Change Prompt";
			this.Load += new System.EventHandler(this.VisitChangePromptDlg_Load);
			((System.ComponentModel.ISupportInitialize)(this.dg)).EndInit();
			this.ResumeLayout(false);

		}
		#endregion

		private void VisitChangePromptDlg_Load(object sender, System.EventArgs e)
		{
			DataGridTableStyle tableStyle = new DataGridTableStyle();
			tableStyle.MappingName = "visits";

		}

		public bool InitializeGrid()
		{
			StringBuilder sql = new StringBuilder("SELECT DISTINCT v.ID,v.Patient,v.VisitDate,");
			StringBuilder sb = new StringBuilder("");
			switch (this._table)
			{
				case "ProtocolVisitAdmission":
					this.VisitType.Text = "Admission";
					sql.Append("c.Display AS Category,s.Display AS Site");
					sql.Append(" FROM ProtocolVisitAdmission v,");
					sql.Append(" PCategory_L c,PSite_L s");
					sql.Append(" WHERE v.Protocol=" + this._protocolID);
					sql.Append(" AND v.PCategory=c.ID ");					
					sql.Append(" AND v.PSite=s.ID");
					break;
				case "ProtocolVisitOutPatient":
					this.VisitType.Text = "Out Patient Visit";
					sql.Append("c.Display AS Category,s.Display AS Site");
					sql.Append(" FROM ProtocolVisitOutPatient v,");
					sql.Append(" PCategory_L c,PSite_L s");
					sql.Append(" WHERE v.Protocol=" + this._protocolID);
					sql.Append(" AND v.PCategory=c.ID ");
					sql.Append(" AND v.PSite=s.ID");
					break;
				case "ScatterNurseHours":
					this.VisitType.Text = "Scatter Nurse Hours";
					sql.Append("c.Display AS Category");
					sql.Append(" FROM ScatterNurseHours v,");
					sql.Append(" PCategory_L c");
					sql.Append(" WHERE v.Protocol=" + this._protocolID);
					sql.Append(" AND v.PCategory=c.ID ");
					break;
			}

			//SqlConnection conn = LoginForm.GetSqlConnection();
			DataTable dt = new DataTable("visits");

			try
			{
				DataSet ds = DataAccess.GetDataSet(sql.ToString());
				dt = ds.Tables[0];

				dg.DataSource = dt;
				if (dt.Rows.Count > 0)
					dg.Select(0);
				return true;
			}
			catch (Exception ex)
			{
				sb.Append("Could not execute query!");
				sb.Append(Environment.NewLine);
				sb.Append("Error message: " + ex.Message);
				//bool b = ////ExceptionPolicy.HandleException(ex, "Exception Policy");
				MessageBox.Show(this, sb.ToString(), "Execute Query Failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
				return false;
			}
			finally
			{
				//if(conn != null)
				//    conn.Dispose();
			}
		}

		private void btnExit_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}

		private void btnNoChange_Click(object sender, System.EventArgs e)
		{
			if (dg.CurrentCell.RowNumber != 0)
				return;

			DataTable dt = (DataTable)dg.DataSource;
			if (dt.Rows.Count > 0)
				dt.Rows.Remove(dt.Rows[0]);

			if (dt.Rows.Count == 0)
			{
				this.Close();
				return;
			}

			dg.DataSource = dt;
			dg.Select(0);
			dg.CurrentRowIndex = 0;


		}

		private void btnChange_Click(object sender, System.EventArgs e)
		{
			int id = (int)dg[0, 0];
			StringBuilder sb = new StringBuilder();
			string sql = "UPDATE " + this._table + " SET " + _field +
				"=" + _newVal +
				" WHERE ID=" + id;

			int result = 0;

			//SqlConnection conn = LoginForm.GetSqlConnection();
			try
			{
				result = DataAccess.ExecuteNonQuery(CommandType.Text, sql);
				btnNoChange_Click(sender, e);
			}
			catch (Exception ex)
			{
				sb.Append("Could not update visits!");
				sb.Append(Environment.NewLine);
				sb.Append("Error message: " + ex.Message);
				//bool b = ////ExceptionPolicy.HandleException(ex, "Exception Policy");
				MessageBox.Show(this, sb.ToString(), "Update All Visist Failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
			finally
			{
				//if(conn != null)
				//    conn.Dispose();
			}

			sb.Append("Updated " + result + " record(s).");
			MessageBox.Show(this, sb.ToString(), "Update Visist", MessageBoxButtons.OK, MessageBoxIcon.Information);

		}

	}

}
